package com.shujia.mr;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class kqzltest {
    public static class kqzlMap extends Mapper<LongWritable,Text,Text,LongWritable>{
        @Override
        protected void map(LongWritable key, Text info, Context context) throws IOException, InterruptedException {
            String s = info.toString();
            String[] strings = s.split(",");

            //过滤
            if(strings.length>4 && "PM2.5".equals(strings[2])){
                for(int i = 3,j=1;i<strings.length;i++,j++){
                    //处理空数据
                    if("".equals(strings[i]) || " ".equals(strings[i]) || strings[i]==null){
                        strings[i]="0";
                    }
                    //日期和监测点数目
                    context.write(new Text(strings[0]+","+j),new LongWritable(Long.valueOf(strings[i])));
                }
            }

        }
    }

    public static class kqzlReduce extends Reducer<Text,LongWritable,Text,LongWritable>{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            Long sum = 0L;
            for (LongWritable value : values) {
                long num = value.get();
                sum += num;
            }
            Long avg = sum/24;
            context.write(key,new LongWritable(avg));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //设置输出文件key和value分隔符
        conf.set("mapred.textoutputformat.separator", ",");
        Job job = Job.getInstance(conf,kqzltest.class.getSimpleName());
        //必须指定
        job.setJarByClass(kqzltest.class);
        //数据来自哪里
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //自定义的mapper在哪里
        job.setMapperClass(kqzlMap.class);
        //指定mapper输出的<k2,v2>的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //自定义的reducer在哪里
        job.setReducerClass(kqzlReduce.class);
        //指定reducer输出的<k3,v3>的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //job.setNumReduceTasks(0);
        //数据写到哪里
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }
}
